/*
 *  Copyright 2016 http://www.kedacomframework.org
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *
 */

package com.kedacom.ctsp.authz.oauth2.service;

import com.kedacom.ctsp.authz.oauth2.OAuth2ServerProperties;
import com.kedacom.ctsp.authz.oauth2.entity.AuthorizationCode;
import com.kedacom.ctsp.authz.oauth2.service.code.AuthorizationCodeRequest;
import com.kedacom.ctsp.authz.oauth2.service.code.AuthorizationCodeService;
import com.kedacom.ctsp.lang.mapper.factory.EntityFactory;
import com.kedacom.ctsp.serialize.ISerialization;
import com.kedacom.ctsp.serialize.SerializationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


/**
 * TODO 完成注释
 *
 * @author zhouhao
 */
@Service
@EnableConfigurationProperties(OAuth2ServerProperties.class)
public class AuthorizationCodeServiceImpl implements AuthorizationCodeService {

    @Autowired
    private RedisTemplate<String, byte[]> redisTemplate;
    @Autowired
    private EntityFactory entityFactory;
    @Autowired
    private CodeGenerator codeGenerator;
    @Autowired
    private OAuth2ServerProperties oAuth2ServerProperties;
    @Autowired
    private ISerialization serialization;

    private static final String REDIS_CODE_PREFIX = "_commons_oauth2_code_";


    @Override
    public String create(AuthorizationCodeRequest request) {
        AuthorizationCode codeEntity = entityFactory.newInstance(AuthorizationCode.class);
        String code = codeGenerator.generate();
        codeEntity.setClientId(request.getClientId());
        codeEntity.setRedirectUri(request.getRedirectUri());
        codeEntity.setCreateTime(System.currentTimeMillis());
        codeEntity.setUserId(request.getUserId());
        codeEntity.setScope(request.getScope());
        codeEntity.setCode(code);
        redisTemplate.opsForValue().set(REDIS_CODE_PREFIX + code, SerializationUtil.serialize(serialization, codeEntity), oAuth2ServerProperties.getTokenExpireSeconds(), TimeUnit.SECONDS);
        return codeEntity.getCode();
    }

    @Override
    public AuthorizationCode consume(String code) {
        AuthorizationCode codeEntity = SerializationUtil.deserialize(serialization, redisTemplate.opsForValue().get(REDIS_CODE_PREFIX + code));
        redisTemplate.delete(REDIS_CODE_PREFIX + code);
        return codeEntity;
    }
}
